package com.hxkj.controller;

import com.hxkj.client.service.SiteServiceClient;
import com.hxkj.client.service.TokenServiceClient;
import com.hxkj.client.service.UserSiteCollectServiceClient;
import com.hxkj.client.service.WebsiteAddrServiceClient;
import com.hxkj.domain.enums.UserExceptionEnum;
import com.hxkj.domain.po.Site;
import com.hxkj.domain.po.Token;
import com.hxkj.domain.po.UserSiteCollect;
import com.hxkj.domain.po.WebsiteAddr;
import com.hxkj.entity.vo.SiteType;
import com.hxkj.entity.vo.WebsiteAddrDetail;
import com.hxkj.service.SiteService;
import com.hxkj.service.TokenService;
import com.hxkj.service.UserSiteCollectService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ResultMessageStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * created by kezhijie on 2018/9/5 15:03.
 * email to kezhijie@hxoss.com
 */
@RestController
@RequestMapping("site")
public class SiteController {

    @Autowired
    private SiteService siteService;
    @Autowired
    private UserSiteCollectServiceClient userSiteCollectServiceClient;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserSiteCollectService userSiteCollectService;

    /**
     * 所有站点
     * @return
     * @throws Exception
     */
    @GetMapping("list")
    public List<SiteType> list(HttpServletRequest request) throws Exception {
        String accessToken = request.getHeader("accessToken");
        String userId = getUserIdByToken(accessToken);
        return siteService.getList(userId);
    }

    /**
     * 站点收藏
     * @param userSiteCollect
     * @param request
     * @throws Exception
     */
    @PostMapping("collect")
    public void collect(@ModelAttribute UserSiteCollect userSiteCollect, HttpServletRequest request) throws Exception {
        String accessToken = request.getHeader("accessToken");
        String userId = getUserIdByToken(accessToken);

        userSiteCollect.setUserId(userId);

        if (Objects.isNull(userSiteCollect) || StringUtils.isEmpty(userSiteCollect.getSiteId()))
            throw new CustomException(UserExceptionEnum.COLLECT_SITE_IS_NULL);

        ResultMessageStruct resultMessageStruct = userSiteCollectServiceClient.save(userSiteCollect);
        if (resultMessageStruct.getCode() != 200)
            throw new CustomException(UserExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
    }

    /**
     * 取消站点收藏
     * @param userSiteCollect
     * @param request
     * @throws Exception
     */
    @PostMapping("cancelCollect")
    public void cancelCollect(@ModelAttribute UserSiteCollect userSiteCollect, HttpServletRequest request) throws Exception {
        String accessToken = request.getHeader("accessToken");
        String userId = getUserIdByToken(accessToken);

        userSiteCollect.setUserId(userId);

        if (Objects.isNull(userSiteCollect) || StringUtils.isEmpty(userSiteCollect.getSiteId()))
            throw new CustomException(UserExceptionEnum.CANCEL_SITE_IS_NULL);

        ResultMessageStruct resultMessageStruct = userSiteCollectServiceClient.cancel(userSiteCollect);
        if (resultMessageStruct.getCode() != 200)
            throw new CustomException(UserExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
    }

    /**
     * 收藏列表
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("collectList")
    public List<WebsiteAddrDetail> collectList(HttpServletRequest request) throws Exception {
        String accessToken = request.getHeader("accessToken");
        String userId = getUserIdByToken(accessToken);
        return userSiteCollectService.collectList(userId);
    }

    private String getUserIdByToken(String accessToken){
        Token token = tokenService.validate(accessToken).getData();
        return token.getUserId();
    }

}
